package com.lemon.cloud.admin.service.impl;

import com.alicp.jetcache.anno.CacheInvalidate;
import com.alicp.jetcache.anno.Cached;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lemon.cloud.admin.service.SysPublicParamService;
import com.lemon.cloud.comm.constants.CacheConstants;
import com.lemon.cloud.admin.entity.SysPublicParam;
import com.lemon.cloud.admin.mapper.SysPublicParamMapper;
import org.springframework.stereotype.Service;

@Service
public class SysPublicParamServiceImpl extends ServiceImpl<SysPublicParamMapper, SysPublicParam> implements SysPublicParamService {

    @Override
    @Cached(name = CacheConstants.PARAMS_DETAILS, key = "#key")
    public String getSysPublicParamKeyToValue(String key) {
        SysPublicParam sysPublicParam = this.baseMapper
                .selectOne(Wrappers.<SysPublicParam>lambdaQuery().eq(SysPublicParam::getKey, key));

        if (sysPublicParam != null) {
            return sysPublicParam.getValue();
        }
        return null;
    }

    @Override
    @CacheInvalidate(name = CacheConstants.PARAMS_DETAILS, key = "#param.key")
    public Boolean removeParam(SysPublicParam param) {
        if (param == null) {
            return true;
        }
        this.removeById(param.getId());
        return true;
    }

    @Override
    @CacheInvalidate(name = CacheConstants.PARAMS_DETAILS, key = "#param.key")
    public boolean updateById(SysPublicParam param) {
        return super.updateById(param);
    }

    @Override
    @CacheInvalidate(name = CacheConstants.PARAMS_DETAILS, key = "#key")
    public Boolean sync(String key) {
        return true;
    }
}
